/**
 * ServicePlanAction.java
 * Created at 2017-11-20
 * Created by zhanghailin
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.system.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.system.model.TmServicePlan;
import com.broadtext.rms.system.service.IServicePlanService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: ServicePlanAction</p>
 * <p>Description: TODO</p>
 * <p>Author: zhanghailin</p>
 * <p>Date: 2017-11-20</p>
 */
@Controller
@RequestMapping("/main/servicePlan")
public class ServicePlanAction {
    
    @Autowired
    @Qualifier("servicePlanService")
    private IServicePlanService servicePlanService;
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/getServicePlan.mvc")
    @ResponseBody
    public Map<String,Object> getServicePlan(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
        PageBounds pageBounds = new PageBounds();
        //解析请求参数，将String字符串转为map
        Map<String,Object> map = new HashMap<String,Object>();
        String aoData = request.getParameter("aoData");
        JSONArray array = JSONArray.parseArray(aoData);
        if (array!=null) {
            Iterator<Object> it = array.iterator();
            while (it.hasNext()) {
                JSONObject obj;
                obj = (JSONObject) it.next();
                if (obj.get("value") != null && StringUtils.isNotBlank(obj.get("value")+"")) {
                    map.put(obj.get("name") + "", obj.get("value"));
                }
            }
        }
        //分页查询条数
        pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
        //分页第几页
        pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit() + 1);
        Map<String, Object> dataMap = new HashMap<String, Object>();
        try {
            EntityPageBean<TmServicePlan> pageBean = this.servicePlanService.findEntityPageByQuery(map, pageBounds);
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return dataMap;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/findCustomerOpt.mvc")
    @ResponseBody
    public Map<String, Object> findCustomerOpt (HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        List<Object> list;
        try {
            list = this.servicePlanService.findCustomerOpt();
            map.put("dict", list);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/getUserNamebyLoginName.mvc")
    @ResponseBody
    public Map<String, Object> getUserNamebyLoginName (HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();
        String loginName = String.valueOf(request.getSession().getAttribute("sys.userName"));
        if(StringUtils.isBlank(loginName)||"null".equals(loginName)){
            loginName = request.getParameter("loginName");
        }
        try {
            map = this.servicePlanService.getUserNamebyLoginName(loginName);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param pkId
     * @return
     * @throws ServiceException 
     */
    @RequestMapping(value="/getPlanById.mvc")
    @ResponseBody
    public TmServicePlan getPlanById(String pkId) throws ServiceException {
        TmServicePlan t = new TmServicePlan();
        t = this.servicePlanService.getPlanById(pkId);
        return t;
    }
    
    
    
    /**
     * 
     * <p>Description: TODO</p>
     * @param request
     * @param response
     * @param TmCustomAlert
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/saveOrUpdateServicePlan.mvc")
    @ResponseBody
    public Map<String, Object> saveOrUpdateServicePlan(HttpServletRequest request, HttpServletResponse response,TmServicePlan t) throws ServiceException {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String currentUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        if(StringUtils.isBlank(currentUser)||"null".equals(currentUser)){
            currentUser = t.getCurrentUser();
        }
        t.setDelFlag("0");
        t.setUpdateUser(currentUser);
        t.setUpdateDate(new Date());
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(StringUtils.isNotBlank(t.getPlanStartDateStr())){
                t.setPlanStartDate(format.parse(t.getPlanStartDateStr()));
            }
            if(StringUtils.isNotBlank(t.getPlanEndDateStr())){
                t.setPlanEndDate(format.parse(t.getPlanEndDateStr()));
            }
            //if (!"".equals(t.getPkId())) {
            if(StringUtils.isNotBlank(t.getPkId())){
                //编辑
                t.setUpdateUser(currentUser);
                t.setUpdateDate(new Date());
                this.servicePlanService.updateRecord(t);
            } else {
                //新增
                //初始化服务计划状态 1 未处理 2 已确认 3处理中 4 已解决 5 已关闭 
                t.setPlanStatus("1");
                t.setCreateUser(currentUser);
                t.setCreateDate(new Date());
                t.setPkId(UUIDUtils.getUUID());
                this.servicePlanService.saveServicePlan(t);
            }
            dataMap.put("msg", "SUCCESS");
        } catch (ServiceException e) {
            dataMap.put("msg", "FAILURE");
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dataMap;
    }
    
    
    /**
     * <p>
     * Description: 删除
     * </p>
     * 
     * @param request 请求
     * @param pkId id
     * @param response 返回对象
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value = "/deleteServicePlan.mvc")
    @ResponseBody
    public Object delete(HttpServletRequest request, String pkId, HttpServletResponse response) {
        //返回成功
        Map dataMap;
        dataMap = new HashMap();
        //数组 存储待删除主键id
        String[] idArray;
        idArray = pkId.split(",");
        try {
            this.servicePlanService.deleteServicePlan(idArray, String.valueOf(request.getSession().getAttribute("sys.userName")));
            dataMap.put("msg", "SUCCESS");
        } catch (ServiceException e) {
            dataMap.put("msg", "FAILURE");
            e.printStackTrace();
        }
        return dataMap;
    }
    

    /**
     * 
     * <p>
     * Description: 检查请求编码是否重复
     * </p>
     * 
     * @param requestCode
     * @param pkId
     * @return Map<String, Object>
     * @throws ServiceException 异常
     */
    @RequestMapping(value = "/checkRepeatPlanCode.mvc")
    @ResponseBody
    public Map<String, Object>  checkRepeatPlanCode(String planCode, String pkId) throws ServiceException {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean flag = this.servicePlanService.checkRepeatPlanCode(planCode, pkId);
        if(flag){
            map.put("msg", true);
        }else {
            map.put("msg", false);
        }
        return map;
    }


}
